using System.Collections.Generic;
using UnityEngine;

public class BotTimerExtreme : BotHoleController
{
	public Hole targetHole;

	public float agressiveTimer;

	private bool runAway;

	private bool isOverAgressive;

	protected override void Awake()
	{
		base.Awake();
		isOverAgressive = (Random.value < 0.2f);
	}

	private void Update()
	{
		if (!BaseGameManager.Instance.HasGameStarted || BaseGameManager.Instance._hasGameEnded)
		{
			return;
		}
		if ((bool)targetObject && targetObject.IsDestroyed)
		{
			target = null;
		}
		if (_hole.IsDead)
		{
			if (farTarget == null)
			{
				farTarget = FindFurthestTarget();
				if ((bool)farTarget)
				{
					changeTargetTimer = Time.time + 5f;
				}
			}
			if ((bool)farTarget)
			{
				Vector3 vector = Vector3.MoveTowards(_cachedTransform.position, farTarget.position, Time.deltaTime * _hole.Speed * _sensitivity);
				Vector3 position = _cachedTransform.position;
				vector.y = position.y;
				SetDirection(vector - _cachedTransform.position);
				_cachedTransform.position = vector;
				if (changeTargetTimer - Time.time < 0f)
				{
					farTarget = null;
				}
			}
			return;
		}
		if ((bool)targetHole)
		{
			if (targetHole.IsDead)
			{
				targetHole = null;
				agressiveTimer = Time.time + Random.Range(2f, 5f);
			}
			else if (targetHole.Size < _hole.Size)
			{
				Vector3 vector2 = Vector3.MoveTowards(_cachedTransform.position, targetHole.CurrentPosition, Time.deltaTime * _hole.Speed * _sensitivity);
				Vector3 position2 = _cachedTransform.position;
				vector2.y = position2.y;
				SetDirection(vector2 - _cachedTransform.position);
				_cachedTransform.position = vector2;
				if (changeTargetTimer - Time.time < 0f)
				{
					if (targetHole == BaseGameManager.Instance.CurrentHole)
					{
						agressiveTimer = Time.time + Random.Range(10f, 15f);
					}
					else
					{
						agressiveTimer = Time.time + Random.Range(3f, 5f);
					}
					targetHole = null;
				}
			}
			else
			{
				targetHole = null;
				agressiveTimer = Time.time + Random.Range(3f, 10f);
			}
			return;
		}
		if ((bool)target)
		{
			if (DestroyTargetIfOutOfBounds())
			{
				return;
			}
			Vector3 vector3 = Vector3.MoveTowards(_cachedTransform.position, target.position, Time.deltaTime * _hole.Speed * _sensitivity);
			Vector3 position3 = _cachedTransform.position;
			vector3.y = position3.y;
			SetDirection(vector3 - _cachedTransform.position);
			_cachedTransform.position = vector3;
			if (changeTargetTimer - Time.time < 0f)
			{
				target = null;
				if (runAway)
				{
					runAway = false;
				}
			}
			if (AreHoleAround() && !runAway)
			{
				target = FindFurthestTarget();
				if (target != null)
				{
					changeTargetTimer = Time.time + Random.Range(2f, 5f);
					runAway = true;
				}
			}
			return;
		}
		if (agressiveTimer - Time.time < 0f)
		{
			targetHole = FindClosestHole();
		}
		if (targetHole != null)
		{
			if (targetHole != BaseGameManager.Instance.CurrentHole)
			{
				Transform transform = FindClosestTarget();
				if ((bool)transform && (transform.position - _cachedTransform.position).sqrMagnitude < (targetHole.CurrentPosition - _cachedTransform.position).sqrMagnitude)
				{
					targetHole = null;
					targetObject = transform.GetComponent<FallingObjectBehaviour>();
					target = transform;
				}
			}
			changeTargetTimer = Time.time + Random.Range(5f, 10f);
			return;
		}
		if (isOverAgressive)
		{
			target = FindClosestTarget();
			if (target != null)
			{
				targetObject = target.GetComponent<FallingObjectBehaviour>();
				changeTargetTimer = Time.time + Random.Range(5f, 10f);
			}
			return;
		}
		float value = Random.value;
		if (value < 0.7f)
		{
			target = FindClosestTarget();
			if (target != null)
			{
				targetObject = target.GetComponent<FallingObjectBehaviour>();
				changeTargetTimer = Time.time + Random.Range(5f, 10f);
			}
		}
		else
		{
			target = FindFurthestTarget();
			if (target != null)
			{
				targetObject = target.GetComponent<FallingObjectBehaviour>();
				changeTargetTimer = Time.time + Random.Range(3f, 6f);
			}
		}
	}

	private Hole FindClosestHole()
	{
		List<Hole> currentHoles = BaseGameManager.Instance.CurrentHoles;
		Hole hole = null;
		foreach (Hole item in currentHoles)
		{
			if (!(item == null) && !(item == _hole) && !(item.Size >= _hole.Size) && !item.IsDead && (hole == null || (item.CurrentPosition - _cachedTransform.position).sqrMagnitude < (hole.CurrentPosition - _cachedTransform.position).sqrMagnitude))
			{
				hole = item;
			}
		}
		if (hole == null)
		{
			return null;
		}
		return hole;
	}
}
